<?php
class Post_model extends CI_Model{
	var $_tableName = 'data';
	var $arrThumbSize = array(
			'150x150' => 'crop',
			'460x460' => 'landscape',
		);
    function __construct(){
        // Call the Model constructor
        parent::__construct();
    }
    public function getLatestPost($offset = 0, $limit = '', $offsetId = 0){
    	if($limit == '') $limit = HOME_LIMIT;

    	$this->db->select('id, like_count, share_count, view, post_title, post_image, crawl_date, item_type, category_id, source_page, video_code');
        if($offsetId > 0){
            $this->db->where('id < ', $offsetId); 
        }
        $this->db->order_by("id", "desc");
    	$query = $this->db->get($this->_tableName, $limit, $offset);
        return $query->result();
    }
    public function getPostDetail($id)
    {
        $this->db->select('id, post_title, post_image, crawl_date, item_type, category_id, source_page, video_code');
        $query = $this->db->get_where($this->_tableName, array('id' => $id));
        return $query->result();
    }
    public function navigationPost($postId)
    {
        $sqlNav = 'SELECT id, post_title FROM '.$this->_tableName.' WHERE id = (SELECT MIN(id) FROM '.$this->_tableName.' where id > '.$postId.') OR id = (SELECT MAX(id) FROM '.$this->_tableName.' where id < '.$postId.')';
        $query = $this->db->query($sqlNav);
        return $query->result();
    }
    public function updatePostView($postId)
    {
        $sqlView = "UPDATE ".$this->_tableName." SET view = view + 1 WHERE id='$postId'";
        $query = $this->db->query($sqlView); 
    }
    public function getPostByCate($cateId, $offset = 0, $limit = '', $offsetId = 0){
        if($limit == '') $limit = CATE_LIMIT;
        $this->db->select('id, view, like_count, share_count, post_title, post_image, crawl_date, item_type, category_id, source_page, video_code');
        $this->db->where('category_id', $cateId); 
        if($offsetId > 0){
            $this->db->where('id < ', $offsetId); 
        }
        $this->db->order_by("id", "desc");

        $query = $this->db->get($this->_tableName, $limit, $offset);
        return $query->result();
    }
    public function getOlderPost($currentId, $limit = 9, $offset = 0)
    {
        $this->db->select('id, post_title, post_image');
        $this->db->where('id <', $currentId);
        $this->db->order_by("id", "desc");
        $query = $this->db->get($this->_tableName, $limit, $offset);
        return $query->result();
    }
	public function doFileUpload($fileName, $fileTmpPath){
		$todayFolder = date('Y/m/d/i/');
		try{
			if(!file_exists(IMAGE_PATH.$todayFolder)) mkdir(IMAGE_PATH.$todayFolder, 0777, true);
			if(move_uploaded_file($fileTmpPath, IMAGE_PATH.$todayFolder.$fileName)){
				self::_generateThumbnail(array('path' => $todayFolder, 'name' => $fileName));
				return $todayFolder.$fileName;
			}
		}
		catch(Exception $e){
			return false;
		}
	}
	private function _generateThumbnail($arrFile){
		$path = null;
		$arrDim = null;
		$this->load->library('Imageresize', array(IMAGE_PATH.$arrFile['path'].$arrFile['name'])); 
		foreach($this->arrThumbSize as $size => $name){
			$arrDim = explode('x', $size);
			$path = IMAGE_THUMB_PATH.$size.'/'.$arrFile['path'];
			if(!is_dir($path)){
				@mkdir($path, 0777, true);
			}
			if(!isset($arrDim[1]) || $arrDim[1] == '') $arrDim[1] = $arrDim[0];
			//$resizeObj = new Imageresize(IMAGE_PATH.$arrFile['path'].$arrFile['name']);
			// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
			$this->imageresize->resizeImage($arrDim[0], $arrDim[1], $name);
			// *** 3) Save image
			$this->imageresize->saveImage($path.$arrFile['name'], 100);
		}
	}
	public function savePost($params){
		try{
			$this->db->insert($this->_tableName, $params);
			return $this->db->insert_id();
		}
		catch(Exception $e){
			return false;
		}
	}
}